/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.couponrule.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.couponinfo.entity.CouponInfo;
import com.thinkgem.jeesite.modules.couponinfo.service.CouponInfoService;
import com.thinkgem.jeesite.modules.couponrule.entity.CouponRule;
import com.thinkgem.jeesite.modules.couponrule.service.CouponRuleService;

/**
 * 生成加息券规则Controller
 * @author thinkgem
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/couponrule/couponRule")
public class CouponRuleController extends BaseController {

	@Autowired
	private CouponInfoService couponInfoService;
	
	@Autowired
	private CouponRuleService couponRuleService;
	
	@ModelAttribute
	public CouponRule get(@RequestParam(required=false) String id) {
		CouponRule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = couponRuleService.get(id);
		}
		if (entity == null){
			entity = new CouponRule();
		}
		return entity;
	}
	
	@RequiresPermissions("couponrule:couponRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(CouponRule couponRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CouponRule> page = couponRuleService.findPage(new Page<CouponRule>(request, response), couponRule); 
		model.addAttribute("page", page);
		return "modules/couponrule/couponRuleList";
	}

	@RequiresPermissions("couponrule:couponRule:view")
	@RequestMapping(value = "form")
	public String form(CouponRule couponRule, Model model) {
		model.addAttribute("couponRule", couponRule);
		List<CouponInfo> couponInfoList = couponInfoService.findList(new CouponInfo());
		model.addAttribute("couponInfoList", couponInfoList);
		return "modules/couponrule/couponRuleForm";
	}

	@RequiresPermissions("couponrule:couponRule:edit")
	@RequestMapping(value = "save")
	public String save(CouponRule couponRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, couponRule)){
			return form(couponRule, model);
		}
		if (couponRule.getIsNewRecord()) {
			couponRule.setCreateTime(new java.util.Date());
		}else {
			couponRule.setModifyTime(new java.util.Date());
		}
		if (couponRule.getType() != null) {
			CouponInfo couponInfo = couponInfoService.get(couponRule.getType());
			if (couponInfo != null) {
				couponRule.setName(couponInfo.getCouponName());
				couponRule.setProductType(couponInfo.getProductType());
			}
		}
		couponRuleService.save(couponRule);
		addMessage(redirectAttributes, "保存加息券规则成功");
		return "redirect:"+Global.getAdminPath()+"/couponrule/couponRule/?orderBy=createTime desc";
	}
	
	@RequiresPermissions("couponrule:couponRule:edit")
	@RequestMapping(value = "delete")
	public String delete(CouponRule couponRule, RedirectAttributes redirectAttributes) {
		couponRuleService.delete(couponRule);
		addMessage(redirectAttributes, "删除加息券规则成功");
		return "redirect:"+Global.getAdminPath()+"/couponrule/couponRule/?orderBy=createTime desc";
	}

}